package com.myapp.demo.controller;

import com.gitee.fastmybatis.core.PageInfo;
import com.gitee.fastmybatis.core.query.LambdaQuery;
import com.gitee.fastmybatis.core.query.Query;
import com.myapp.demo.model.TUser;
import com.myapp.demo.service.UserService;
import org.junit.Assert;
import org.noear.solon.annotation.Controller;
import org.noear.solon.annotation.Inject;
import org.noear.solon.annotation.Mapping;

import java.util.Arrays;
import java.util.List;

@Controller
public class DemoController {

    @Inject
    private UserService userService;

    /**
     * http://localhost:6041/index
     *
     * @return
     */
    @Mapping("index")
    public TUser index() {
        return userService.getById(6);
    }

    /**
     * http://localhost:6041/index2
     *
     * @return
     */
    @Mapping("index2")
    public List<TUser> index2() {
        Query query = new Query()
                .in("id", Arrays.asList(4, 5, 6));
        return userService.list(query);
    }

    /**
     * http://localhost:6041/test
     *
     * @return
     */
    @Mapping("test")
    public String test() {
        try {
            lambda();
        } catch (Exception e) {
            e.printStackTrace();
            return e.getMessage();
        }
        return "OK";
    }

    private void lambda() {
        List<TUser> users = userService.query()
                .eq(TUser::getUsername, "张三")
                .list();
        Assert.assertTrue(users.size() > 0);

        TUser tUser = userService.getById(6);
        System.out.println(tUser);
        Assert.assertNotNull(tUser);

        LambdaQuery<TUser> query3 = userService.query()
                .eq(TUser::getUsername, 1);

        LambdaQuery<TUser> query = userService.query()
                .page(1, 10)
                .eq(TUser::getId, 6);

        PageInfo<TUser> page = userService.page(query);
        Assert.assertTrue(page.getTotal() == 1);
        Assert.assertTrue(page.getList().size() > 0);

        TUser user = userService.get(query);
        Assert.assertNotNull(user);

        TUser user1 = userService.get(TUser::getUsername, "张三");
        Assert.assertNotNull(user1);

        String columnValue = userService.getValue(userService.query().eq(TUser::getId, 6), TUser::getUsername);
        System.out.println("columnValue:" + columnValue);
        Assert.assertNotNull(columnValue);

        List<Integer> list1 = Arrays.asList(1, 2, 3, 6);
        List<TUser> tUsers = userService.list(list1);
        System.out.println(tUsers);
        Assert.assertTrue(tUsers.size() > 0);

        LambdaQuery<TUser> query1 = userService.query()
                .in(TUser::getId, Arrays.asList(1, 2, 6));
        List<String> username = userService.listValue(query1, TUser::getUsername);
        System.out.println(username);
        Assert.assertTrue(username.size() > 0);

        LambdaQuery<TUser> lambdaQuery = userService.query()
                .set(TUser::getUsername, "aa")
                .set(TUser::getMoney, 33)
                .setExpression("add_time=now()")
                .eq(TUser::getId, 6);
        int i = userService.update(lambdaQuery);
        Assert.assertEquals(1, i);

        LambdaQuery<TUser> updateQuery2 = userService.query()
                .set(TUser::getUsername, "王五")
                .set(TUser::getRemark, "aaa")
                .setExpression("add_time=now()")
                .eq(TUser::getId, 6);

        int j = userService.update(updateQuery2);
        Assert.assertEquals(1, j);

        boolean exist = userService.checkExist(TUser::getId, 6);
        Assert.assertTrue(exist);

        LambdaQuery<TUser> lambdaQuery1 = userService.query()
                // 只查询id, username两列
                .select(TUser::getId, TUser::getUsername)
                .eq(TUser::getId, 6);
        List<TUser> tUsers1 = userService.list(lambdaQuery1);
        System.out.println(tUsers1);
        Assert.assertTrue(tUsers1.get(0).getId() != null);
        Assert.assertTrue(tUsers1.get(0).getUsername() != null);
        Assert.assertTrue(tUsers1.get(0).getState() == null);

        // QUERY

        // 根据条件查询
        List<TUser> list = userService.query()
                .eq(TUser::getUsername, "张三")
                .list();
        Assert.assertTrue(list.size() > 0);

        PageInfo<TUser> pageInfo = userService.query()
                .eq(TUser::getUsername, "张三")
                .orderByDesc(TUser::getId)
                .paginate(1, 10);
        Assert.assertTrue(pageInfo.getTotal() > 0);
        Assert.assertTrue(pageInfo.getList().size() > 0);


        int cnt2 = userService.query()
                .eq(TUser::getUsername, "张三")
                .deleteForce();
        Assert.assertTrue(cnt2 > 0);

        int update = userService.query()
                .set(TUser::getUsername, "李四")
                .setExpression("add_time=now()")
                .eq(TUser::getId, 6)
                .update();
        Assert.assertTrue(update > 0);


        int i1 = userService.query()
                .set(TUser::getRemark, "被xx删除")
                .eq(TUser::getId, 1)
                .delete();
        Assert.assertEquals(1, i1);


        int i2 = userService.getMapper().query()
                .eq(TUser::getId, 1)
                .deleteForce();
        Assert.assertTrue(i2 > 0);
    }
}
